﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Modules;
    using System.Linq;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmDmDichVu : DevExpress.XtraEditors.XtraForm
    {
        IList<DmDichVuEntity> lstDmDichVuEntity = null;
        DmDichVuBusiness dmDichVuBusiness = new DmDichVuBusiness();
        GridCheckMarksSelection gridCheckMarksSelection = null;

        public frmDmDichVu()
        {
            InitializeComponent();
            gridCheckMarksSelection = new GridCheckMarksSelection(gvDichVu);
            LoadLoaiDichVu();
        }

        void LoadLoaiDichVu()
        {
            lkLoaiDichVu.Properties.DataSource = LoaiPhieu.LoaiPhieuEntity();
            lkLoaiDichVu.Properties.DisplayMember = "TenLoaiPhieu";
            lkLoaiDichVu.Properties.ValueMember = "MaLoaiPhieu";
            lkLoaiDichVu.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadDataGrid(object sender, EventArgs e)
        {
            lstDmDichVuEntity = dmDichVuBusiness.GetDmDichVuList();
            if (lkLoaiDichVu.EditValue.ToString() != ConstantManagerClinic.sValueMember)
            {
                lstDmDichVuEntity = lstDmDichVuEntity.Where(n => n.MaLoaiDichVu == lkLoaiDichVu.EditValue.ToString()).ToList();
            }
            grdDichVu.DataSource = lstDmDichVuEntity;
        }

        private void frmDmDichVu_Load(object sender, EventArgs e)
        {
            Authenticate.IsAuthenticateForm(this);
            LoadDataGrid(sender, e);
        }

        private void lkLoaiDichVu_EditValueChanged(object sender, EventArgs e)
        {
            LoadDataGrid(sender, e);
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            var frm = new frmCapNhatDmDichVu((int)ConstantManagerClinic.StateInsertOrUpdate.Insert);
            frm.ButtonClicked += new EventHandler(LoadDataGrid);
            frm.ShowDialog();
        }

        DmDichVuEntity getDataDmDichVuEntity()
        {
            if (gvDichVu.RowCount > 0)
            {
                return (DmDichVuEntity)gvDichVu.GetFocusedRow();
            }

            return null;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            var dmDichVuEntity = getDataDmDichVuEntity();
            if (dmDichVuEntity == null) return;
            var frm = new frmCapNhatDmDichVu((int)ConstantManagerClinic.StateInsertOrUpdate.Update, dmDichVuEntity);
            frm.ButtonClicked += new EventHandler(LoadDataGrid);
            frm.ShowDialog();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            var deleteObjects = gridCheckMarksSelection.selection;

            if (deleteObjects.Count <= 0)
            {
                MessageBox.Show("Bạn chưa chọn bản ghi để xóa!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }

            if (MessageBox.Show("Bạn chắc chắn muốn xóa dữ liệu?", ConstantManagerClinic.sTitleWarming, MessageBoxButtons.YesNo) == DialogResult.No)
            {
                gridCheckMarksSelection.ClearSelection();
                return;
            }

            foreach (DmDichVuEntity dm in deleteObjects)
            {
                dmDichVuBusiness.DeleteDmDichVu(dm.MaDichVu);
            }

            gridCheckMarksSelection.ClearSelection();
            LoadDataGrid(sender, e);
        }

        private void gvDichVu_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
                e.Handled = false;
            }
        }

        private void grdDichVu_DoubleClick(object sender, EventArgs e)
        {
            if (btnSua.Enabled == false) return;
            btnSua_Click(sender, e);
        }

    }
}